<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-21
 * Time: 下午8:58
 * 地图模式控制器
 */
namespace Service\Controller;
class MapController extends BaseController {

    //地图
    public  function  index(){
        $re        = strsToArray(session("location"));
        $location  = $re[0].",".$re[1];
        $map['location']   = array('like','%'.$location.'%');
        //$map['plant_type'] = 1;

        //预设站点
        $map['current_state'] = array('neq',1);
        $yushe=M("plant")->where($map)->select();
        $this->assign("yushe",count($yushe));

        //所有站点
        $map['current_state'] = 1;
        $plant=M("plant")->where($map)->select();
        $this->assign("count",count($plant));
        //实时站点
        $map['is_active'] = 1;
        $shishi=M("plant")->where($map)->select();
        $this->assign("shishi",count($shishi));
        //$this->coordinate = "116.490816,39.968931";
        $this->china = D("Admin/Region")->getDowntown($re['0']);//当前省下所有市区县
        $this->display();
    }
    public  function command(){
        $this->display();
    }
    //初始查询地图坐标
    public  function map_sel(){
//        $plant=M()->query('select * from ev_plant where current_state=1 and city like "成都%"');
        $re                     = strsToArray(session("location"));
        $location               = $re[0].",".$re[1];
        $map['location']        = array('like',$location.'%');
        //$map['plant_type']      = 1;
        $map['current_state']   = 1;
        $map['is_active']       = 1;
        $plant = M("plant")->where($map)->select();
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //查询所有站点地图坐标
    public  function map_now(){
        $re                     = strsToArray(session("location"));
        $location               = $re[0].",".$re[1];
        $map['location']        = array('like',$location.'%');
        //$map['plant_type']      = 1;
        $plant = M("plant")->where($map)->select();
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //预设站点
    public  function map_will(){
//        $plant=M()->query('select id,name,current_state,address,is_active,coordinate from ev_plant where current_state<>1 and is_active<>1');
        $re                     = strsToArray(session("location"));
        $location               = $re[0].",".$re[1];
        $map['location']        = array('like',$location.'%');
        //$map['plant_type']      = 1;
        $map['current_state']   = array("neq",1);
        $map['is_active']       = array("neq",1);
        $plant = M("plant")->where($map)->select();
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //站点模糊查询
    public  function map_name(){
        if(!empty($_GET['name'])){
            $name = I("name");
        }
//        $plant=M()->query("select id,name,current_state,address,is_active,coordinate from ev_plant where name like '%".$name."%'");
//        echo M()->getLastSql();die;
        $re                     = strsToArray(session("location"));
        $location               = $re[0].",".$re[1];
        $map['location']        = array('like',$location.'%');
        $map['name']            = array('like','%'.$name.'%');
        //$map['plant_type']      = 1;
        $plant = M("plant")->where($map)->select();
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //省市区站点查询
    public  function map_loc(){

        if(!empty($_GET['location_f_1'])){
            if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
        }
        $plant=M()->query("select id,name,current_state,address,is_active,coordinate from ev_plant where location like '%$location%' ");
//        echo M()->getLastSql();die;
        $AddMarkString = '';
        for( $i=0;$i<count($plant);$i++){
            if(!empty($plant[$i]['coordinate'])){
                $arr=explode(",",$plant[$i]['coordinate']);
                $AddMarkString .="mark".$plant[$i]['id'].",".$arr[0].",".$arr[1].",".$plant[$i]['name'].",".$plant[$i]['current_state'].",".$plant[$i]['address'].",".$plant[$i][id].",".$plant[$i]['is_active'].",".$plant[$i]['current_state']."+";
            }
        }
        if($AddMarkString){
            echo $AddMarkString;
        }else{
            echo error_get_last();
        }
    }

    //查询当前站点，电桩，状态
    public function map_sel_state_count(){

        $plant_id=$_POST['plant_id'];
        //此站点下所有桩与其对应的状态
        $charInfo = M("char")->where("ev_plant_id='".$plant_id."'")->select();
        if($charInfo){
            foreach($charInfo as $k=>$v){
                $plantInfo = M("plant")->where("id='".$v['ev_plant_id']."'")->find();
                $stateInfo = D("Admin/State")->where("key_link='".$v['key_link']."'")->find();
                $charInfo[$k]['title']       = $plantInfo['name'];
                $charInfo[$k]['address']     = $plantInfo['address'];
                $fac_name = M("fac")->where("fac_login_name='".$v['ev_fac_code']."'")->find();
                if($fac_name['fac_name'] != ''){
                    $charInfo[$k]['trademark']   = $fac_name['fac_name'];
                }else{
                    $charInfo[$k]['trademark']   = "未知";
                }
                $charInfo[$k]['station_key'] = $plantInfo['station_key'];
                $charInfo[$k]['stateInfo']   = $stateInfo;
            }
            echo json_encode($charInfo);
        }
    }

}